package com.ruoyi.psi.sale.collection.service;

import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.psi.sale.collection.domain.CollectionList;
import com.ruoyi.psi.sale.collection.domain.SaleCollection;
import com.ruoyi.psi.sale.collection.mapper.CollectionMapper;
import com.ruoyi.psi.sale.order.mapper.SaleOrderMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售收款
 * 业务执行层
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@Service
public class CollectionServiceImpl implements ICollectionService {
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 查询所有销售收款
     *
     * @param collection 搜索条件
     * @return
     */
    @Override
    public List<SaleCollection> selectCollectionList(SaleCollection collection) {
        List<SaleCollection> collections = saveCollectionLists(collectionMapper.selectCollectionList(collection));
        return collections;
    }

    /**
     * 存入对应销售收款详细
     * @param collections 销售收款数组
     * @return
     */
    public List<SaleCollection> saveCollectionLists(List<SaleCollection> collections){
        for (int i = 0; i < collections.size(); i++) {
            //存入对应销售收款详细
            collections.get(i).setCollectionLists(setOtherParameters(collectionMapper.selectCollectionListListById(collections.get(i).getCollectionId())));
        }
        return collections;
    }

    /**
     * 存入出库金额，已收款金额
     * @param collectionLists 销售收款详细数组
     * @return
     */
    public List<CollectionList> setOtherParameters(List<CollectionList> collectionLists){
        for (CollectionList c:collectionLists) {
            switch (c.getCollectionType()){
                case "0":
                    //单据类型：销售订单
                    c.setSalePriceTotal(collectionMapper.getSalePriceTotalForOrder(c.getSaleOrderId()));
                    //出库金额
                    c.setSaleDeliveryPrice(collectionMapper.getSaleDeliveryPriceById(c.getSaleOrderId()));
                    break;
                case "1":
                    //单据类型：销售退回
                    c.setSalePriceTotal(collectionMapper.getSalePriceTotalForReturn(c.getSaleOrderId()));
                    //出库金额 = 单据金额
                    c.setSaleDeliveryPrice(c.getSalePriceTotal());
                    break;
            }
            //存入已收款项
            c.setReceivedAmount(collectionMapper.getAmountReceived(c));
        }
        return collectionLists;
    }

    /**
     * 根据编号查询
     *
     * @param collectionId 销售收款编号
     * @return
     */
    @Override
    public SaleCollection selectCollectionById(String collectionId) {
        return collectionMapper.selectCollectionById(collectionId);
    }

    /**
     * 新增销售收款
     *
     * @param collection
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCollection(SaleCollection collection) throws Exception{
        if(StringUtils.isNotNull(collectionMapper.selectCollectionById(collection.getCollectionId())) && StringUtils.isNull(collection.getCollectionLists())){
            //已存在编号或无销售收款详细
            return 0;
        }
        String collectionId = idUtil.createCollectionId();//自动生成编号
        collection.setCollectionId(collectionId);//主表添加编号
        collectionMapper.insertCollection(collection);
        for (int i = 0; i < collection.getCollectionLists().size(); i++) {
            collection.getCollectionLists().get(i).setCollectionId(collectionId);// 子表添加编号
            collectionMapper.insertCollectionList(collection.getCollectionLists().get(i));
            // 判断订单收款情况的修改
            // 订单金额 - 出库金额 《= 收款金额
//            SaleOrderDto saleOrder = saleOrderService.selectSaleOrderById(collection.getCollectionLists().get(i).getSaleOrderId());
            if (collection.getCollectionLists().get(i).getSalePriceTotal() - collection.getCollectionLists().get(i).getSaleDeliveryPrice() <= collection.getCollectionLists().get(i).getCollectionAmount()) {
                saleOrderMapper.updateCollectionStatus(collection.getCollectionLists().get(i).getSaleOrderId());
            }
        }
        return 1;
    }

    /**
     * 更新销售收款
     *
     * @param collection
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCollection(SaleCollection collection) throws Exception{
        if(!StringUtils.isNotNull(collectionMapper.selectCollectionById(collection.getCollectionId())) && StringUtils.isNull(collection.getCollectionLists())){
            //不存在编号或无销售收款详细
            return 0;
        }
        collectionMapper.updateCollection(collection);
        List<CollectionList> collectionListsOld = collectionMapper.selectCollectionListListById(collection.getCollectionId());
        List<CollectionList> collectionListsNew = collection.getCollectionLists();
        for (int i = 0; i < collectionListsNew.size(); i++) {
            //查询数据库此条销售详细是否存在
            CollectionList collectionList = collectionMapper.selectCollectionListById(collectionListsNew.get(i));
            if (StringUtils.isNotNull(collectionList)){
                //存在
                for (int j = 0; j < collectionListsOld.size(); j++) {
                    if (collectionListsOld.get(j).getSaleOrderId().equals(collectionList.getSaleOrderId())){
                        //移除旧有中相同部分
                        collectionListsOld.remove(i);
                        break;
                    }
                }
                //更新
                collectionMapper.updateCollectionList(collectionListsNew.get(i));
            }else {
                //不存在，新增
                collectionMapper.insertCollectionList(collectionListsNew.get(i));
            }
        }
        for (int i = 0; i < collectionListsOld.size(); i++) {
            collectionMapper.deleteCollectionList(collectionListsOld.get(i));
        }
        return 1;
    }

    /**
     * 删除销售收款
     *
     * @param collection
     * @return
     */
    @Override
    public int updateCollectionDeleteFlag(SaleCollection collection) {
        return collectionMapper.updateCollectionDeleteFlag(collection);
    }

    /**
     * 新增销售收款时查询指定客户对应的销售订单以及销售退货单据
     *
     * @param customerInfoId 客户编号
     * @return
     */
    @Override
    public List<CollectionList> getCollectionBill(String customerInfoId) {
        List<CollectionList> collectionLists = collectionMapper.getCollectionBill(customerInfoId);
        for (CollectionList c:collectionLists) {
            switch (c.getCollectionType()){
                case "0":
                    //单据类型：销售订单
                    //出库金额
                    c.setSaleDeliveryPrice(collectionMapper.getSaleDeliveryPriceById(c.getSaleOrderId()));
                    break;
                case "1":
                    //单据类型：销售退回
                    //单据金额
                    c.setSalePriceTotal(-c.getSalePriceTotal());
                    //出库金额 = 单据金额
                    c.setSaleDeliveryPrice(c.getSalePriceTotal());
                    break;
            }
            //存入已收款项
            c.setReceivedAmount(collectionMapper.getAmountReceived(c));
            //预存入本次收款金额 = 出库金额 - 已收款项
            c.setCollectionAmount(c.getSaleDeliveryPrice() - c.getReceivedAmount());
        }
        return collectionLists;
    }
}
